package xyz.xiangyushui.controller.admin.employee;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xyz.xiangyushui.dao.admin.AssignEmployeeDao;


/**
 * Servlet implementation class AssignEmployeeValidater
 */
@WebServlet("/admin/employee/assignPost")
public class AssignEmployeeValidater extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final String emptyWarning = "Incomplete form!";
	private final String successMsg="employee successfully assigned!";
	private AssignEmployeeDao aed;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AssignEmployeeValidater() {
        super();
        aed=new AssignEmployeeDao();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String employee=request.getParameter("employee");
		String store=request.getParameter("store");
		String msg=validate(employee, store);
		if(msg.equals(successMsg)){
			aed.add(employee, Integer.parseInt(store.substring(0, store.indexOf('-'))));
			request.setAttribute("successMsg", msg);
		}else{
			request.setAttribute("failedMsg", msg);
		}
		request.getRequestDispatcher("/admin/employee/assign").forward(request, response);
	}

	private String validate(String employee,String store) {
		String result = successMsg;
		if ("".equals(employee) || "".equals(store)) {
			result = emptyWarning;
		} 
		return result;
	}
}
